package patrol.mobile.com.mobilepatrol.activity.route_query;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.DBHelper;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.StringUtils;

import static patrol.mobile.com.mobilepatrol.manager.UserManager.getUser;

/**
 * Created by apple on 18/1/23.
 */

public class RouteActivity extends BaseActivity {
    private DBHelper db;
    @BindView(R.id.el)
    ExpandableListView el;

    @BindView(R.id.tv_org)
    TextView tv_org;

    @BindView(R.id.tv_mail)
    TextView tv_mail;


    private TypeAdapter mAdapter;
    private List<RouteType> mList;
    private int from;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_route;
    }

    @Override
    public void setListener() {
        switch (from){
            case 1:
                break;
            case 2:
                el.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        Intent intent = new Intent();
                        intent.putExtra("route",mList.get(groupPosition).routeManages.get(childPosition));
                        setResult(1,intent);
                        CommonUtil.exitActivityAndBackAnim(RouteActivity.this,true);
                        return false;
                    }
                });
                break;
        }

    }

    @Override
    public void initData() {
        super.initData();
        from = getIntent().getIntExtra("from",-1);
        db = new DBHelper(this);
        tv_org.setText(getUser().bmname);
        tv_mail.setText("共"+String.format("%.2f", db.getRouteManageMiles())+"km");
        List<String> types = db.getRouteManageTypes(UserManager.getUser().bmname);
        if (types.size()==0){
            showToast("请初始化路线查询单位信息");
            return;
        }
        List<String> sortTypes = StringUtils.sortList(db.getRouteManageTypes(UserManager.getUser().bmname));
        mList = db.getTypes(sortTypes,UserManager.getUser().bmname);
         mAdapter = new TypeAdapter(this,mList);
        el.setAdapter(mAdapter);
        //遍历所有group,将所有项设置成默认展开
        for (int i=0; i<mList.size(); i++)
        {
            el.expandGroup(i);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(RouteActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("管养路线");
    }
}
